﻿using Sunny.UI;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PressureTest
{
    public partial class ManageTemplateForm : Form
    {
        /// <summary>模型
        /// 
        /// </summary>
        private Template obj = new Template();

        /// <summary>DataGridView
        /// 
        /// </summary>
        private DataGridView dataGridView;

        /// <summary>窗口禁止调整大小
        /// 
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref System.Windows.Forms.Message m)
        {
            if (m.Msg == 0x112)
            {
                switch ((int)m.WParam)
                {
                    //禁止双击标题栏关闭窗体
                    case 0xF063:
                    case 0xF093:
                        m.WParam = IntPtr.Zero;
                        break;

                    //禁止拖拽标题栏还原窗体
                    case 0xF012:
                    case 0xF010:
                        m.WParam = IntPtr.Zero;
                        break;

                    //禁止双击标题栏
                    case 0xf122:
                        m.WParam = IntPtr.Zero;
                        break;

                    //允许关闭按钮
                    case 0xF060:
                        //m.WParam = IntPtr.Zero;
                        break;

                    //允许最大化按钮
                    case 0xf020:
                        //m.WParam = IntPtr.Zero;
                        break;

                    //允许最小化按钮
                    case 0xf030:
                        //m.WParam = IntPtr.Zero;
                        break;

                    //禁用还原按钮
                    case 0xf120:
                        //m.WParam = IntPtr.Zero;
                        break;

                    default:
                        //m.WParam = IntPtr.Zero;
                        break;
                }
            }
            base.WndProc(ref m);
        }

        /// <summary>设备点位
        /// 
        /// </summary>
        public void LoadDevicePointList()
        {
            List<Template> list = SqlSugarHelper.Inquireable<Template>(null);
            UpdateDataGridViewRow(list);
            this.HideWaitForm();
        }

        /// <summary>更新DataGridView
        /// 
        /// </summary>
        /// <param name="list"></param>
        private void UpdateDataGridViewRow(List<Template> list)
        {
            if (dataGridView.Rows.Count > 1)
            {
                this.Invoke(new Action(() =>
                {
                    dataGridView.Rows.Clear();
                }));
            }

            foreach (Template item in list)
            {
                DataGridViewRow row = new DataGridViewRow();
                row.Cells.Add(dataGridViewBoxCell(item.number.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.pipe_position));
                row.Cells.Add(dataGridViewBoxCell(item.design_pressure.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.first_stage_pressure.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.first_stage_minute.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.second_stage_pressure.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.second_stage_minute.ToString()));
                string pressure = item.third_stage_pressure.ToString();
                if (item.third_stage_pressure == 0)
                {
                    pressure = "--";
                }
                row.Cells.Add(dataGridViewBoxCell(pressure));
                string minute = item.third_stage_minute.ToString();
                if (item.third_stage_minute == 0)
                {
                    minute = "--";
                }
                row.Cells.Add(dataGridViewBoxCell(minute));
                row.Cells.Add(dataGridViewBoxCell(item.rigor_stage_pressure.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.rigor_stage_minute.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.intensity_stage_pressure.ToString()));
                row.Cells.Add(dataGridViewBoxCell(item.intensity_stage_minute.ToString()));
                string edit = "否";
                if (item.edit_enable == 1)
                {
                    edit = "是";
                }
                row.Cells.Add(dataGridViewBoxCell(edit));
                row.Height = 40;
                this.Invoke(new Action(() =>
                {
                    dataGridView.Rows.Add(row);
                }));
            }
        }

        /// <summary>刷新
        /// 
        /// </summary>
        public void Fresh()
        {
            LoadDevicePointList();
        }

        // <summary> 创建DataGridViewTextBoxCell
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private DataGridViewTextBoxCell dataGridViewBoxCell(string str)
        {
            DataGridViewTextBoxCell cell1 = new DataGridViewTextBoxCell();
            cell1.Value = str;
            return cell1;
        }

        public ManageTemplateForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.Manual;
            Screen primaryScreen = Screen.PrimaryScreen;
            this.MaximizedBounds = primaryScreen.WorkingArea;
            this.MinimumSize = primaryScreen.WorkingArea.Size;
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
        }

        /// <summary>添加模板
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddTemplateButton_Click(object sender, EventArgs e)
        {
            AddTemplateForm frm = new AddTemplateForm();
            frm.Owner = this;
            frm.ShowDialog();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void ManageTemplateForm_Load(object sender, EventArgs e)
        {
            this.dataGridView = this.TemplateDataGridView;
            this.TemplateDataGridView.Height = this.Height - this.ToolStrip1.Height - 40;
            Fresh();
        }

        /// <summary>编辑
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditMenu_Click(object sender, EventArgs e)
        {
            bool res = CheckSelectedRowExist();
            if (!res)
            {
                return;
            }
            EditTemplateForm frm = new EditTemplateForm();
            frm.obj.number = obj.number;
            frm.Owner = this;
            frm.ShowDialog();
        }

        /// <summary>删除
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteMenu_Click(object sender, EventArgs e)
        {
            bool res = CheckSelectedRowExist();
            if (!res)
            {
                return;
            }
            res = this.ShowAskDialog2("友情提示", "您确定要删除吗?", true);
            if (res)
            {

                List<Template> list = new List<Template>();
                list.Add(obj);
                int deleteRes = SqlSugarHelper.Deleteable(list);
                if (deleteRes > 0)
                {
                    Fresh();
                }
            }
        }

        /// <summary>判断记录是否存在
        /// 
        /// </summary>
        /// <param name="number"></param>
        private bool CheckSelectedRowExist()
        {
            int number = SelectedRowPrimaryKey();
            obj = TemplateWithNumber(number);
            if (obj == null)
            {
                this.ShowErrorDialog2("记录不存在");
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>选中记录主键值
        /// 
        /// </summary>
        /// <returns></returns>
        private int SelectedRowPrimaryKey()
        {
            int index = this.dataGridView.CurrentRow.Index;
            return this.dataGridView.Rows[index].Cells[0].Value.ToString().ToInt();
        }

        /// <summary>根据编号查询记录
        /// 
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        private Template TemplateWithNumber(int number)
        {
            Dictionary<string, Object> where = new Dictionary<string, Object>();
            where["number"] = number;
            List<Template> list = SqlSugarHelper.Inquireable<Template>(where);
            if (list.Count > 0)
            {
                return list[0];
            }
            else
            {
                return null;
            }
        }


        private void TemplateDataGridView_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            int index = e.ColumnIndex;
            if (index < 0)
            {
                index = 0;
            }
            dataGridView.ClearSelection();
            dataGridView.Rows[e.RowIndex].Selected = true;
            dataGridView.CurrentCell = dataGridView.Rows[e.RowIndex].Cells[index];
            OperationMenu.Show(MousePosition.X, MousePosition.Y);
        }
    }
}
